package com.qc31.monitorlib.ui.adapter

import android.view.View
import android.widget.LinearLayout
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.custom.LeftRightTextView
import com.qc31.comlib.entity.HistoryLine
import com.qc31.monitorlib.R

/** @Author Jasper @Time 2020/8/12 @Desc 轨迹适配器 */
class DataAdapter<T>(private val type: Int) :
    BaseQuickAdapter<T, BaseViewHolder>(R.layout.item_data) {
    override fun convert(holder: BaseViewHolder, item: T) {
        if (holder.layoutPosition < 9) {
            holder.setText(R.id.tv_item_num, "0${(holder.layoutPosition + 1).toString()}")
        } else {
            holder.setText(R.id.tv_item_num, (holder.layoutPosition + 1).toString())
        }
        val itemOne: LeftRightTextView = holder.getView(R.id.lrv_item_one)
        val itemTwo: LeftRightTextView = holder.getView(R.id.lrv_item_two)
        val itemThree: LeftRightTextView = holder.getView(R.id.lrv_item_three)
        val itemFour: LeftRightTextView = holder.getView(R.id.lrv_item_four)
        val llItemFour: LinearLayout = holder.getView(R.id.ll_item_four)
        val llItemSix: LinearLayout = holder.getView(R.id.ll_item_six)

        llItemSix.isVisible = type == 2
        when (type) {
            0 -> {
                (item as? HistoryLine.StopData)?.apply {
                    llItemFour.visibility = View.GONE

                    itemOne.setLeftTitle(R.string.desc_choose_start_time)
                    itemTwo.setLeftTitle(R.string.desc_choose_end_time)
                    itemThree.setLeftTitle(R.string.desc_stop_time)
                    itemOne.setRightText(startTime)
                    itemTwo.setRightText(endTime)
                    itemThree.setRightText(long)
                    holder.setText(R.id.tv_item_five_value, add)

                }
            }
            1 -> {
                (item as? HistoryLine.AlarmData)?.apply {
                    llItemFour.visibility = View.VISIBLE
                    itemOne.setLeftTitle(R.string.desc_alarm_type)
                    itemTwo.setLeftTitle(R.string.desc_choose_start_time)
                    itemThree.setLeftTitle(R.string.desc_data_handle_time)
                    itemFour.setLeftTitle(R.string.desc_continue_time)
                    itemOne.setRightText(alarmType)
                    itemTwo.setRightText(startTime)
                    itemThree.setRightText(solveTime ?: "")
                    itemFour.visibility = View.VISIBLE
                    itemFour.setRightText(long)
                    holder.setText(R.id.tv_item_four, R.string.desc_data_now_time)
                    holder.setText(
                        R.id.tv_item_four_value, String.format(
                            "%s %s",
                            speed,
                            context.getString(R.string.unit_speed)
                        )
                    )
                    holder.setText(R.id.tv_item_five_value, addr)
                }
            }
            2 -> {
                (item as? HistoryLine.OverspeedData)?.apply {
                    itemOne.setLeftTitle(R.string.desc_speed_max)
                    itemTwo.setLeftTitle(R.string.desc_choose_start_time)
                    itemThree.setLeftTitle(R.string.desc_choose_end_time)
                    itemFour.setLeftTitle(R.string.desc_data_speed_time)

                    itemOne.setRightText("$maxSpeed km/h")
                    itemTwo.setRightText(startTime)
                    itemThree.setRightText(endTime)
                    itemFour.setRightText(long)

                    holder.setText(R.id.tv_item_four, R.string.desc_data_speed_mile)
                    holder.setText(R.id.tv_item_four_value, mile)
                    holder.setText(R.id.tv_item_five, R.string.desc_start_addr)
                    holder.setText(R.id.tv_item_five_value, startAddr)
                    holder.setText(R.id.tv_item_six, R.string.desc_end_addr)
                    holder.setText(R.id.tv_item_six_value, endAddr ?: "")
                }
            }
        }
    }
}